/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.util.monitors;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.ProgressMonitor;
import javax.swing.UIManager;

/**
 *
 * @author yura
 */
public class SimpleProgresMonitorImpl extends JDialog
        implements SimpleProgresMonitor {

    private JProgressBar myBar;
    private JLabel noteLabel;
    private String message;
    private Object[] cancelOption;
    private JOptionPane pane;

    public SimpleProgresMonitorImpl(String message) {
        this(null, message);
    }

    public SimpleProgresMonitorImpl(JFrame parent, String msg) {
        super(parent, msg, true);

        message = msg;
        myBar = new JProgressBar();
        cancelOption = new Object[1];
        cancelOption[0] = UIManager.getString("OptionPane.cancelButtonText");
        noteLabel = new JLabel(msg);

        myBar.setStringPainted(true);
        myBar.setMinimum(MIN);
        myBar.setMaximum(MAX);

        pane = new JOptionPane(getContentData(),
                JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION,
                null, cancelOption, null);
        noteLabel.setLabelFor(this);

        Container contentPane = getContentPane();

        contentPane.setLayout(new BorderLayout());
        contentPane.add(pane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(parent);
        addWindowListener(new WindowAdapter() {

            boolean gotFocus = false;

            @Override
            public void windowClosing(WindowEvent we) {
                pane.setValue(cancelOption[0]);
            }

            @Override
            public void windowActivated(WindowEvent we) {
                if (!gotFocus) {
                    pane.selectInitialValue();
                    gotFocus = true;
                }
            }
        });

        addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (isVisible()
                        && event.getSource() == pane
                        && (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)
                        || event.getPropertyName().equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
                    setVisible(false);
                    dispose();
                }
            }
        });

    }

    protected Object[] getContentData() {
        return new Object[]{message, noteLabel, myBar};
    }

    @Override
    public void setNoteAndProgress(String note, int prog) {
        setNote(note);
        setProgress(prog);
    }

    @Override
    public void setNote(String note) {
        noteLabel.setText(note);
    }

    @Override
    public void setProgress(int prog) {
        myBar.setValue(prog);
    }

    @Override
    public boolean isCanceled() {
        if (pane == null) {
            return false;
        }

        Object v = pane.getValue();
        return ((v != null) && (cancelOption.length == 1) && (v.equals(cancelOption[0])));
    }

    @Override
    public void close() {
        setVisible(false);
        dispose();
        pane = null;
        myBar = null;
    }

    @Override
    public void reset() {
        pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
    }
}
